package com.ruoyi.ybUpload.service.upload.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.ybUpload.domain.MainForm;
import com.ruoyi.ybUpload.domain.QueryParams;
import com.ruoyi.ybUpload.domain.tableCode.TraceCode;
import com.ruoyi.ybUpload.domain.tableCode.TraceCodeDetail;
import com.ruoyi.ybUpload.domain.jxc.*;
import com.ruoyi.ybUpload.mapper.TraceBaseInfoMapper;
import com.ruoyi.ybUpload.mapper.TraceCodeDetailMapper;
import com.ruoyi.ybUpload.mapper.TraceCodeMapper;
import com.ruoyi.ybUpload.service.upload.YBUploadInterfaceService;
import com.ruoyi.ybUpload.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class YBUploadInterfaceServiceImpl implements YBUploadInterfaceService {

    @Autowired
    private TraceCodeMapper traceCodeMapper;
    @Autowired
    private TraceCodeDetailMapper traceCodeDetailMapper;
    @Autowired
    private TraceBaseInfoMapper traceBaseInfoMapper;


    @Override
    public MainForm structureMainForm(String interfaceId) {
        CommonUtils commonUtils = new CommonUtils();

        MainForm mainForm = new MainForm();
        // 接口编号
        mainForm.setInfno(interfaceId);
        // 定点医药机构编号(12)+时间(14)+顺序号(4) 时间格式：yyyyMMddHHmmss
        String dateTime = commonUtils.getDateTimeString("无杠");
        mainForm.setMsgid("H45120200008" + dateTime + "0001");
        // 就医地医保区划
        mainForm.setMdtrtarea_admvs("451299");
        // 接收方系统代码
        mainForm.setRecer_sys_code("451299");
        // 接口版本号
        mainForm.setInfver("V1.0");
        // 经办人类别 1-经办人；2-自助终端；3-移动终端
        mainForm.setOpter_type("1");
        // 工号
        mainForm.setOpter("3654");
        // 姓名
        mainForm.setOpter_name("韦凯文");
        // 交易时间
        mainForm.setInf_time(commonUtils.getDateTimeString("有杠"));
        // 机构编码
        mainForm.setFixmedins_code("H45120200008");
        // 机构名称
        mainForm.setFixmedins_name("河池市人民医院");
        //登录码
        mainForm.setSign_no("451200G0000080413319");

        return mainForm;
    }

    @Override
    public MainForm structure3501(QueryParams queryParams) {
        //3501基本信息
        TraceCode traceCode = new TraceCode();
        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());

        //Domain3501 domain3501 = traceCodeMapper.selectInvinfo(traceCode);
        //todo 查询3501
        Domain3501 domain3501 = traceBaseInfoMapper.select3501BaseInfo(queryParams.getFixmedinsBchno());

        List<Map<String,String>> codeList = getTraceCodeDetailInfo(traceCode);
        domain3501.setDrugtracinfo(codeList);

        //盘存日期,生产日期,有效期止
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            domain3501.setInvdate(dateFormat.parse(domain3501.getInvdate_String()));
            domain3501.setManu_date(dateFormat.parse(domain3501.getManu_date_String()));
            domain3501.setExpy_end(dateFormat.parse(domain3501.getExpy_end_String()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //主表单信息
        MainForm mainForm3501 = structureMainForm("3501");
        Map<String, Object> map3501 = new HashMap<>();
        map3501.put("invinfo", domain3501);
        mainForm3501.setInput(map3501);
        return mainForm3501;
    }

    @Override
    public MainForm structure3502(QueryParams queryParams, String invChgType) {
        //3502基本信息
        TraceCode traceCode = new TraceCode();
        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());
        Domain3502 domain3502 = traceCodeMapper.selectInvinfo3502(traceCode);
        //todo 查询3502
        //traceBaseInfoMapper.select3502BaseInfo(queryParams.getFixmedinsBchno());

        domain3502.setInv_chg_type(invChgType);
        String InvChgTime = domain3502.getInv_chg_time_string()+ " " + "00" + ":" + "00" + ":" + "00";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = dateFormat.parse(InvChgTime);
            domain3502.setInv_chg_time(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Map<String,String>> codeList = getTraceCodeDetailInfo(traceCode);
        domain3502.setDrugtracinfo(codeList);

        //主表单信息
        MainForm mainForm3502 = structureMainForm("3502");
        Map<String, Object> map3502 = new HashMap<>();
        map3502.put("invinfo", domain3502);
        mainForm3502.setInput(map3502);
        return mainForm3502;
    }

    @Override
    public MainForm structure3503(QueryParams queryParams) {
        //3503基本信息
        TraceCode traceCode = new TraceCode();
        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());
        Domain3503 domain3503 = traceCodeMapper.selectInvinfo3503(traceCode);
        //todo 查询3503
        //traceBaseInfoMapper.select3503BaseInfo(queryParams.getFixmedinsBchno());

        //生产日期,有效期止, 采购入库时间
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String PurcRetnstoinTime = domain3503.getPurc_retn_stoin_time_string()+ " " + "00" + ":" + "00" + ":" + "00";
            domain3503.setPurc_retn_stoin_time(dateTime.parse(PurcRetnstoinTime));
            domain3503.setManu_date(date.parse(domain3503.getManu_date_string()));
            domain3503.setExpy_end(date.parse(domain3503.getExpy_end_string()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //主表单信息
        MainForm mainForm3503 = structureMainForm("3503");
        Map<String, Object> map3503 = new HashMap<>();
        map3503.put("purcinfo", domain3503);
        mainForm3503.setInput(map3503);
        return mainForm3503;
    }

    @Override
    public MainForm structure3505(QueryParams queryParams) {
        //3505基本信息
        TraceCode traceCode = new TraceCode();
        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());
        Domain3505 domain3505 = traceCodeMapper.selectInvinfo3505(traceCode);
        //todo 查询3505
        //traceBaseInfoMapper.select3505BaseInfo(queryParams.getFixmedinsBchno());

        //生产日期,有效期止, 采购入库时间
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String PurcRetnstoinTime = domain3505.getSel_retn_time_string()+ " " + "00" + ":" + "00" + ":" + "00";
            domain3505.setSel_retn_time(dateTime.parse(PurcRetnstoinTime));
            domain3505.setManu_date(date.parse(domain3505.getManu_date_string()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //主表单信息
        MainForm mainForm3505 = structureMainForm("3505");
        Map<String, Object> map3505 = new HashMap<>();
        map3505.put("selinfoDetail", domain3505);
        mainForm3505.setInput(map3505);
        return mainForm3505;
    }


    @Override
    public MainForm structure3504(QueryParams queryParams) {
        //3504基本信息
        TraceCode traceCode = new TraceCode();
        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());
        Domain3504 domain3504 = traceCodeMapper.selectInvinfo3504(traceCode);
        //todo 查询3504
        //traceBaseInfoMapper.select3504BaseInfo(queryParams.getFixmedinsBchno());

        //生产日期,有效期止, 采购入库时间
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String purcRetnstoinTime = domain3504.getPurc_retn_stoin_time()+ " " + "00" + ":" + "00" + ":" + "00";
            domain3504.setSel_retn_time(dateTime.parse(purcRetnstoinTime));
            domain3504.setManu_date(date.parse(domain3504.getManu_date_string()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //主表单信息
        MainForm mainForm3504 = structureMainForm("3504");
        Map<String, Object> map3504 = new HashMap<>();
        map3504.put("selinfoDetail", domain3504);
        mainForm3504.setInput(map3504);
        return mainForm3504;
    }

//    @Override
//    public MainForm structure3506(QueryParams queryParams) {
//        //3506基本信息
//        TraceCode traceCode = new TraceCode();
//        traceCode.setFixmedinsBchno(queryParams.getFixmedinsBchno());
//        Domain3506 domain3506 = traceCodeMapper.selectInvinfo3506(traceCode);
//        //todo 查询3506
//        //traceBaseInfoMapper.select3506BaseInfo(queryParams.getFixmedinsBchno());
//
//        //生产日期,有效期止, 采购入库时间
//        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            String PurcRetnstoinTime = domain3506.getSel_retn_time_string()+ " " + "00" + ":" + "00" + ":" + "00";
//            domain3506.setSel_retn_time(dateTime.parse(PurcRetnstoinTime));
//            domain3506.setManu_date(date.parse(domain3506.getManu_date_string()));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//        //主表单信息
//        MainForm mainForm3506 = structureMainForm("3506");
//        Map<String, Object> map3506 = new HashMap<>();
//        map3506.put("selinfoDetail", domain3506);
//        mainForm3506.setInput(map3506);
//        return mainForm3506;
//    }

    /**
     * 整合方法接口3508-3509-3510通用
     *method08_10
     */
    private Map<String, Object> method08_10(QueryParams queryParams){
        Domain3508 domain3508 = new Domain3508();
        domain3508.setFixmedins_code("H45120200008");
        domain3508.setFixmedins_bchno(queryParams.getFixmedinsBchno());
        domain3508.setBegndate(queryParams.getBegndate());
        domain3508.setEnddate(queryParams.getEnddate());

        Map<String, Object> map = new HashMap<>();
        map.put("data", domain3508);
        return map;
    }

    @Override
    public MainForm structure3508(QueryParams queryParams) {
        MainForm mainForm3508 = structureMainForm("3508");
        mainForm3508.setInput(method08_10(queryParams));
        return mainForm3508;
    }

    @Override
    public MainForm structure3509(QueryParams queryParams) {
        MainForm mainForm3509 = structureMainForm("3509");
        mainForm3509.setInput(method08_10(queryParams));
        return mainForm3509;

    }

    @Override
    public MainForm structure3510(QueryParams queryParams) {
        MainForm mainForm3510 = structureMainForm("3510");
        mainForm3510.setInput(method08_10(queryParams));
        return mainForm3510;
    }

    @Override
    public MainForm structure3513(QueryParams queryParams) {
        MainForm mainForm3513 = structureMainForm("3513");
        Domain3513 domain3513 = new Domain3513();
        domain3513.setFixmedins_code("H45120200008");
        domain3513.setMdtrtSetlType(queryParams.getMdtrtSetlType());
        domain3513.setPsnCertType(queryParams.getPsnCertType());

        Map<String, Object> map = new HashMap<>();
        map.put("data", domain3513);
        mainForm3513.setInput(map);
        return mainForm3513;
    }


    @Override
    public  MainForm structure9001(QueryParams queryParams) {
        MainForm mainForm9001 = structureMainForm("9001");
        Map<String, Object> map9001 = new HashMap<>();
        Domain9001 domain9001 = new Domain9001();
        domain9001.setOpter_no(queryParams.getOpter_no());
        domain9001.setMac(queryParams.getMac());
        domain9001.setIp(queryParams.getIp());
        map9001.put("signIn", domain9001);
        mainForm9001.setInput(map9001);
        return mainForm9001;
    }


    private List<Map<String,String>> getTraceCodeDetailInfo(TraceCode traceCode){
        TraceCodeDetail detail = new TraceCodeDetail();
        detail.setFixmedinsBchno(traceCode.getFixmedinsBchno());
        List<Map<String,String>> list = traceCodeDetailMapper.selectDrug_trac_codgList(detail);
        if (list.size() > 0){
            return list;
        }
        return null;
    }
}
